package com.yjd.comm.util;

import com.yjd.comm.order.dto.ReturnFeeDto;
import com.yjd.comm.project.model.ProjectInteresModel;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.vo.Constant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 分期利率表
 * Created by yjd-wap on 2017/8/14.
 */
public class StagesInterestUtils {

    /**
     * 获取所有项目应还金额（本金+利息）
     * @param list
     * @param limit
     * @return
     * @throws Exception
     */
    public static Double getAllRepayment(List<ProjectInteresModel> list, Integer limit)throws Exception{
           Double repayment = 0d;
            if(list==null||list.size()==0){
                FrameUtil.throwBaseException1("利率请求参数不能为空");
            }

            if(limit==null){
                FrameUtil.throwBaseException1("请求期数不能为空");
            }

            for(ProjectInteresModel interesModel:list){
                repayment =  ArithUtil.add(repayment,getProjectRepayment(interesModel,limit));
            }

           return repayment;
    }

    /**
     * 计算项目银行金额（本金+利息）
     * @param model
     * @param limit
     * @return
     * @throws Exception
     */
    public static Double getProjectRepayment(ProjectInteresModel model,Integer limit)throws Exception{
        if(model==null){
            FrameUtil.throwBaseException1("请求项目不能为空");
        }
        if(limit==null){
            FrameUtil.throwBaseException1("请求期数不能为空");
        }
        if(model.getLoan_price()==null||model.getLoan_price()==0){
            FrameUtil.throwBaseException1("您的贷款额度必须大于0");
        }

        Double repayment = 0d;
        Double rate = getProjectRate(model,limit);
        Double loan_limit = model.getLoan_price();
        //等额本息计算每月还款额度
        //还款总额 还款本金+利息
        ReturnFeeDto returnFeeDto =  FeeCalcUtil.countA(loan_limit,rate,limit,FrameUtil.getTime(null));
        repayment = ArithUtil.add(loan_limit,returnFeeDto.getInterest());
        return repayment;
    }

    /**
     * 根据项目配置获取项目贷款利率
     * @param model
     * @param limit
     * @return
     * @throws Exception
     */
    public  static Double getProjectRate(ProjectInteresModel model,Integer limit)throws Exception{
        if(model==null){
            FrameUtil.throwBaseException1("请求项目不能为空");
        }
        if(limit==null){
            FrameUtil.throwBaseException1("请求期数不能为空");
        }

        Double rate = 0d;
        if(!StringUtil.isEmpty(model.getRate_config())){
            //获取项目配置的利率金额
            Map rateMap =  StringUtil.getObject(model.getRate_config(), HashMap.class);
            Object obj = rateMap.get(limit+"");
            if(obj!=null){
                rate = Double.parseDouble((String)obj);
            }
        }

        if(rate==null||rate==0d){
            //项目利率配置未获取到则需要获取系统参数配置利率
            //获取用户贷款比例,生成订单
            Constant.SysParamKeyEnum sysParamKeyEnum = StagesRateUtils.getSysParamKeyEnum(limit);
            if (sysParamKeyEnum == null) {
                FrameUtil.throwBaseException1("您选择的贷款期数不合法");
            }
            //获取贷款利率
             rate = Double.parseDouble(SysParamUtil.getValue(sysParamKeyEnum));
        }

        if(rate==null||rate==0d){
            FrameUtil.throwBaseException1("您选择的贷款期数不存在");
        }

        return rate;
    }

    public static List<Integer> getMfqPeriodsConfig()throws Exception{
        String config = SysParamUtil.getValue(Constant.SysParamKeyEnum.MFQ_PERIODS_CONFIG);
        if(StringUtil.isEmpty(config)){
            FrameUtil.throwBaseException1("系统期数参数配置错误");
        }
        List<Integer> periodsList = StringUtil.getObject(config,List.class);
        return periodsList;
    }

    public static void main(String[] args) {

        try {
//            ProjectInteresModel model = new ProjectInteresModel();
//            model.setLoan_price(10000D);
//            Map aa = new HashMap();
//            aa.put("12","18");
//            model.setRate_config(JSON.toJSONString(aa));

            System.out.println(NumberUtil.formatNumber( ArithUtil.div(ArithUtil.div(ArithUtil.mul(10000,3), 100),
                    12), NumberUtil.FORMAT_4));
          //  System.out.println(getProjectRepayment(model,12));
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
